package com.calvin.lingting.utils;

import com.calvin.lib.notification.NotificationCenter;
import com.calvin.lib.utils.HTTPUtils;
import com.calvin.lib.utils.OnHTTPListener;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Utils {
    public static Executor mExecutor = Executors.newCachedThreadPool();


    public static void downloadFile(String url, final File outFile, final String notice) {
        try {
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            HTTPUtils.get(url, new OnHTTPListener() {
                @Override
                public void onHandler(int code, Map<String, List<String>> header, InputStream inputStream) {
                    if (code == 200) {
                        try {
                            long writeLength = 0;
                            FileOutputStream fileOutputStream = new FileOutputStream(outFile);
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            while ((len = inputStream.read(buffer)) > 0) {
                                fileOutputStream.write(buffer, 0, len);
                                fileOutputStream.flush();
                                if (notice != null && header.containsKey("content-length")) {
                                    writeLength += len;
                                    long contentLength = Long.valueOf(header.get("content-length").get(0));
                                    NotificationCenter.shared().postNotice(notice, contentLength, writeLength);
                                }
                            }
                            fileOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void downloadFile(String url, final File outFile) {
        downloadFile(url, outFile, null);
    }

    public static boolean serialization(String path, Object object) {
        if (path != null && object != null && object instanceof Serializable) {
            try {
                File file = new File(path);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
                objectOutputStream.writeObject(object);
                objectOutputStream.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static Object unSerialization(String path) {
        if (path != null) {
            File file = new File(path);
            if (file.exists()) {
                try {
                    ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
                    Object reuslt = objectInputStream.readObject();
                    objectInputStream.close();
                    return reuslt;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static <T> T unSerialization(String path, Class<T> clazz) {
        if (clazz != null && path != null) {
            Object result = unSerialization(path);
            return result != null ? clazz.cast(result) : null;
        }
        return null;
    }
}
